package love.contact;

import net.youmi.android.AdManager;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.diy.DiyManager;
import wwk.common.util.SysUtil;
import wwk.common.widget.NavigationBar;
import love.contact.data.Setting;
import love.contact.engine.PhoneService;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MoreActivity extends BaseActivity implements OnItemClickListener
{
	private ListView listView;
	private String[] stringArray;
	private ArrayAdapter<String> adapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        //init youmi AD
        final String YouMiKey="2329a0227994504f";
        final String YouMiSecure="d9c55918bc0a29ce";
        AdManager.getInstance(this).init(YouMiKey,YouMiSecure,false);
        AdManager.getInstance(this).setEnableDebugLog(false);

        setContentView(R.layout.common_nav_list_activity);
        
        NavigationBar navigationBar=(NavigationBar)this.findViewById(R.id.navBar);

		String title=getString(R.string.more);
        setTitle(title);
        navigationBar.setTitle(title);

		listView=(ListView)this.findViewById(android.R.id.list);
		listView.setCacheColorHint(0);
		Drawable color=new ColorDrawable(0xFF000000);
		listView.setDivider(color);
		listView.setDividerHeight(1);
		listView.setTextFilterEnabled(true);
		
		registerForContextMenu(listView);  
		
		//listener
		listView.setOnItemClickListener(this);

        stringArray=new String[1];

 		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringArray)//stringArray锟斤拷锟斤拷锟斤拷null
 				{
		 			@Override
		 			public int getCount() 
		 			{
		 				return 6;
		 			}
		
		 			@Override
		 			public String getItem(int position) 
		 			{
		 				return ""+position;
		 			}
		
		 			@Override
		 			public long getItemId(int position) 
		 			{
		 				return position;
		 			}
		
		 			@Override
		 			public View getView(int position, View convertView, ViewGroup parent) {
		 				 if (convertView == null) 
		 		        {
		 		            convertView =  LayoutInflater.from(this.getContext()).inflate(android.R.layout.simple_list_item_1, null);
		 		        }
		 		        
		 				TextView textView=(TextView)convertView;
		 				textView.setTextColor(Color.BLACK);
		 		        int icon=0;
		 		        String text=null;
		 		        
		 		        switch(position)
		 		        {
			 		       case 0:
				 		        {
				 		        	Setting setting=new Setting();
				 		        	setting.load(MoreActivity.this);
				 		        	if(setting.isMonitorServiceEnabled())
				 		            {
					 		        		text=getString(R.string.listenOpened);
					 		        		icon=R.drawable.ic_opened;
					 		        		textView.setTextColor(0xff15950d);
				 		        	}
				 		        	else
				 		        	{
				 		        		text=getString(R.string.listenClosed);
				 		        		icon=R.drawable.ic_closed;
				 		        		textView.setTextColor(Color.RED);
				 		        	}
				 		        }
			 		        	break;
			 		        case 1:
				 		        {
				 		        	icon=R.drawable.ic_mycontact;
				 		        	text=getString(R.string.myContacts);
				 		        }
			 		        	break;
			 		       case 2:
				 		        {
				 		        	icon=R.drawable.ic_mycontact;
				 		        	text=getString(R.string.myBlacks);
				 		        }
			 		        	break;
			 		       case 3:
				 		        {
				 		        	icon=R.drawable.qr;
				 		        	text=getString(R.string.scanQR);
				 		        }
			 		        	break;
			 		       case 4:
				 		       {
				 		    		icon=R.drawable.ic_about_dark;
				 		        	text=getString(R.string.about);
				 		       }
				 		       break;
			 		      case 5:
				 		        {
				 		        	icon=R.drawable.hot;
				 		        	text=getString(R.string.reommendApp);
				 		        }
			 		        	break;
		 		        }
 
		 		        textView.setText(text);
		 		        textView.setTextSize(18);
		 		       
		 		        Drawable drawable=this.getContext().getResources().getDrawable(icon);
		 		        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
		 		        textView.setCompoundDrawables(drawable,null,null,null);
		 		        textView.setCompoundDrawablePadding(10);
		
		 		        return convertView;
		 			}
 				};
 		listView.setAdapter(adapter);
 		
 		//youmi banner
 		AdView adView = new AdView(this, AdSize.FIT_SCREEN);
 		ViewGroup v=(ViewGroup)getWindow().getDecorView();
 		v.addView(adView);
    }
	
	public void onResume()
	{
		super.onResume();
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		
//		if(httpEngine!=null)
//		{
//			httpEngine.httpResponseInterface=null;
//			httpEngine.cancel();
//			httpEngine=null;
//		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
	{
		switch(position)
		{
			case 0:
				{
					if(SysUtil.isServiceExists(MoreActivity.this, "love.contact.engine.PhoneService"))
 		            {
						boolean ret=PhoneService.stopService(this);
						if(ret)
						{
							Setting setting=new Setting();
	 		        			setting.load(MoreActivity.this);
	 		        			setting.monitorServiceEnabled=0;
	 		        			setting.save(MoreActivity.this);
						}
 		            }
					else
					{
						boolean ret= PhoneService.startService(this);
						if(ret)
						{
							Setting setting=new Setting();
	 		        			setting.load(MoreActivity.this);
	 		        			setting.monitorServiceEnabled=1;
	 		        			setting.save(MoreActivity.this);
						}
					}
					
					adapter.notifyDataSetChanged();
				}
				break;
			case 1:
				{
					/*
					Intent intent=new Intent("com.android.contacts.action.LIST_CONTACTS");
					Intent intent=new Intent("com.android.phone.action.TOUCH_DIALER");
					Intent intent=new Intent(Intent.ACTION_CALL_BUTTON);
					*/
					
					Intent intent = new Intent(); 
					intent.setAction(Intent.ACTION_VIEW); 
					intent.setData(Contacts.CONTENT_URI); 
					startActivity(intent); 
				}
				break;
			case 2:
				{
					//black some phone number 
				}
				break;
			case 3:
				{
					startActivity(new Intent(this,QrActivity.class));
				}
				break;
			case 4:
				{
					startActivity(new Intent(this,AboutActivtiy.class));
				}
				break;
			case 5:
				{
					DiyManager.showRecommendWall(this);
				}
				break;
		}
	}
}
